package jsu.lipengcheng.trafficnet.controlle.admin;

import jsu.lipengcheng.trafficnet.common.Result;
import jsu.lipengcheng.trafficnet.pojo.News;
import jsu.lipengcheng.trafficnet.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/news")
public class AdminNewsControlle {
    @Autowired
     private NewsService newsService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectbyid(@PathVariable int id) {
        News news = newsService.selectbyid(id);
        return Result.success(news);
    }

    @GetMapping("/all")
    public Result selectall() {
       List<News> newsList= newsService.selectall();
       return Result.success(newsList);
    }

    /**
     * 添加新闻
     * @param news
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody News news) {
        newsService.addnews(news);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Integer> ids) {
        newsService.deletenews(ids);
        return Result.success();
    }

    /**
     * 修改新闻
     * @param news
     * @return
     */
    @PutMapping
    public Result update(@RequestBody News news) {
        newsService.updatanews(news);
        return Result.success();
    }

    @GetMapping("/sum")
    public Result sum() {
       int sum = newsService.selectsum();
       return Result.success(sum);
    }
}
